<?php
use \PsiFramework\Classes\Util\UI\Printer;
use \PsiFramework\Classes\Util\UI\HTML;
use \PsiFramework\Classes\Util\Log\Logger;

$site = $response->getAttribute("object");
$objectType = $response->getAttribute("objectType");
?>
<form
    class="editForm"
    method="POST"
    action="<?php Printer::display(HTML::linkHref('action=CMS.Save&type='.$objectType));?>">
    <?php
    if ($site != null) {
    ?>
    <input
        type="hidden"
        name="id"
        value="<?php Printer::display($site->getId()); ?>" />
    <?php
    }
    ?>
    <div class="label">
        <span class="titre">Nom :</span>
        <input
            class="firstInput"
            type="text"
            name="name"
            value="<?php if ($site != null) Printer::display($site->getName()); ?>" />
    </div>
    <div class="label">
        <span class="titre">URI :</span>
        <input
            class="firstInput"
            type="url"
            name="uri"
            value="<?php if ($site != null) Printer::display($site->getUri()); ?>" />
    </div>
    <div class="label" id="profile">
        <span class="titre">Fiche par défaut :</span>
        <input
            class="firstInput fieldTypeSelecter"
            type="text"
            name="profile_name"
            value="<?php
                if ($site != null && $site->getProfile()!=null) {
                    Printer::display($site->getProfile()->getName());
                }
            ?>" />
        <input
            class="firstInput inputId"
            type="hidden"
            name="profile_id"
            value="<?php 
                if ($site != null && $site->getProfile()!=null) {
                    Printer::display($site->getProfile()->getId()); 
                }
            ?>" />
    </div>
    <div class="label" id="style">
        <span class="titre">Style par défaut :</span>
        <input
            class="firstInput fieldTypeSelecter"
            type="text"
            name="style_name"
            value="<?php
                if ($site != null && $site->getStyle()!=null) {
                    Printer::display($site->getStyle()->getName());
                }
            ?>" />
        <input 
            class="firstInput inputId"
            type="hidden"
            id="style_id"
            name="style_id"
            value="<?php
                if ($site != null && $site->getStyle()!=null) {
                    Printer::display($site->getStyle()->getId());
                }
            ?>" />
    </div>
    <div class="label">
        <span class="titre">Catégories :</span>
        <div
            class="formPartsOrderer"
            id="categories">
            <ul class="formPartsList">
                <?php 
                if ($site != null) {
                    $logger = new Logger("profile");
                    $categories = $site->getCategories();

                    if (!is_null($site) && !is_null($categories)) {
                        for ($i=0; $i<count($categories); $i++) {
                            $part = $categories[$i] ;
                            $category = $part->getCategory();
                            if (!is_null($category)) {
                ?>
                <li>
                    <span class="item">
                        <?php Printer::display($category->getName()); ?>
                        - 
                        <input
                            class="id"
                            type="hidden"
                            name="part_id[]"
                            value="<?php Printer::display($category->getId()); ?>" />
                        <label>
                            Id sur le site : 
                            <input
                                type="text"
                                name="id_for_site[]"
                                size="2"
                                value="<?php Printer::display($part->getIdForSite()); ?>" />
                        </label>
                        <input
                            class="order"
                            type="hidden"
                            name="part_order[]"
                            value="<?php Printer::display($i+1); ?>" />
                    </span>
                </li>
                <?php 
                            }
                        }
                    }
                }
                ?>
            </ul>
        </div>
        <div>
            <?php 
            $siteId = 'null' ;
            if (!is_null($site) && !is_null($site->getId())) {
                $siteId = $site->getId();
            }
            ?>
            <input
                type="button"
                onclick="javascript:retrieveCategories(<?php Printer::display($siteId) ?>)"
                value="Récupérer les catégories depuis le site" /> <span
                id="importMessage"></span>
        </div>
    </div>
    <input type="submit" value="OK" />
</form>
<script type="text/javascript">
    $(document).ready(function() {
        $("#categories .formPartsList").formPartsOrderer(
            {
                "serviceUrl":"?service=ListCategories&format=json",
                "container":"#categories",
                "fields":[
                    {
                        "type":"text",
                        "name":"id_for_site",
                        "label":"Id sur le site",
                        "size":"2"
                    }
                ]
            }
        );

        $("#profile .fieldTypeSelecter").autocomplete({
            source: "?service=ListProfiles&format=json",
            select: function(event, ui) {
                $("#profile").find(".fieldTypeSelecter").val(ui.item.label);
                $("#profile").find(".inputId").val(ui.item.id);
            }
        });

        $("#style .fieldTypeSelecter").autocomplete({
            source: "?service=ListStyles&format=json",
            select: function(event, ui) {
                $("#style").find(".fieldTypeSelecter").val(ui.item.label);
                $("#style").find(".inputId").val(ui.item.id);
            }
        });
        
    });

    function retrieveCategories(id) {
        var style_id = $("#style_id").val();
        if (id == null || id == "" || style_id == null || style_id == "") {
             alert("Il faut d'abord enregistrer le site et un style !");
             return ;
        }else{
            var url = "?service=CreateSiteCategories&site_id=" + id ;
            $.ajax({
                url: url,
                type: 'GET',
                cache: false,
                timeout: 60000,
                beforeSend: function() {
                    $('#importMessage').html('en cours de récupération...')
                },
                complete: function() {
                    $('#importMessage').html('');
                },
                error: function() {
                    return true;
                },
                success: function(data) { 
                    if (data != 'ok') {
                        alert('Erreur lors de la récupération des catégories !');
                    }
                    else {
                        window.location.reload();
                    }
                }
            });
        }
    }
</script>
